﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chaine.DAO;
using MongoDB.Bson;
using Chaine.Models;
using System.Net.Mail;

namespace Chaine.Controllers
{
    public class AdminEmailController : Controller
    {
        //
        // GET: /AdminEmail/

        public ActionResult Index()
        {
            var emailTemplates = EmailTemplateManager.GetEmailTemplates();
            return View("~/views/admin/email/index.cshtml", emailTemplates);
        }

        //
        // GET: /AdminEmail/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AdminEmail/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdminEmail/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminEmail/Edit/5

        public ActionResult Edit(string id)
        {
            var result = EmailTemplateManager.GetEmailTemplate(ObjectId.Parse(id));
            return View("~/views/admin/email/edit.cshtml", result);
        }

        //
        // POST: /AdminEmail/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, EmailTemplateModels email, FormCollection collection)
        {
            try
            {
                ModelState.Clear();
                email.Id = ObjectId.Parse(id);
                try
                {
                    var mailAddress = new MailAddress(email.FromEmail);
                }
                catch (Exception)
                {
                    //email格式不正确
                    ModelState.AddModelError("FromEmail", "Email format is wrong.");
                }

                if (!ModelState.IsValid)
                    return View("~/views/admin/email/edit.cshtml", email);

                EmailTemplateManager.UpdateEmailTemplate(email);
                TempData["update-success"] = "";
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminEmail/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AdminEmail/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
